package com.zr.repertory.areLocation.service.impl;

import com.zr.areLocation.mapper.AreLocationMapper;
import com.zr.areLocation.model.*;
import com.zr.areLocation.service.AreLocationService;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuilderVo;
import com.zr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by 77509 on 2019/11/20.
 */
@Service
public class AreLocationServiceImpl implements AreLocationService {
    @Autowired
    private AreLocationMapper areLocationMapper;
    @Override
    public ResultVo add(@Valid AreLocationAddVo areLocationAddVo) {

        //验证库房标识编码不能重复
        int countCode = areLocationMapper.queryByCode(areLocationAddVo.getWarehouseMarkCode());
        if (countCode > 0) {
            return ResultBuilderVo.error("库房标识编码已经存在！");
        }
        //验证库房标识名称
        ResultVo resultVo = vlidate(areLocationAddVo.getWarehouseMarkName());
        if (!resultVo.getSuccess()) {
            return resultVo;
        }

        //给创建人员，时间等赋值             HttpSession httpSession 中拿
        AreLocationVo areLocationVo = new AreLocationVo();
        //BeanUtils.copyProperties 可以把左边实体类的字段赋值给右边实体类的字段
        //注意：字段名一致才能赋值
        //注意；如果存在集合类型  集合类型必须一致，集合名称也必须一致。才能赋值
        BeanUtils.copyProperties(areLocationAddVo, areLocationVo);
        Date nowDate = new Date();
        areLocationVo.setCreateName("小明");
        areLocationVo.setCreateTime(nowDate);
        areLocationVo.setUpdateName("小明");
        areLocationVo.setUpdateTime(nowDate);
        //入库
        areLocationMapper.add(areLocationVo);
        return ResultBuilderVo.success();

    }

    @Override
    public ResultVo update(@Valid AreLocationUpdateVo areLocationUpdateVo) {

        //验证修改的数据是否存在
       AreLocationVo areLocationVo = areLocationMapper.queryById(areLocationUpdateVo.getId());
       if (areLocationVo == null){
           return ResultBuilderVo.error("修改数据不存在！");
       }
        //验证名称不能重复
        AreLocationVo areLocationVo1 = areLocationMapper.queryAreLocationVoByName(areLocationUpdateVo.getWarehouseMarkName());
        if (areLocationVo1.getId() != areLocationUpdateVo.getId()){
            return ResultBuilderVo.error("库房标识名称已存在！");
        }
        //给创建人员，时间等赋值             HttpSession httpSession 中拿
        AreLocationVo areLocationVo2 = new AreLocationVo();
        //BeanUtils.copyProperties 可以把左边实体类的字段赋值给右边实体类的字段
        //注意：字段名一致才能赋值
        //注意；如果存在集合类型  集合类型必须一致，集合名称也必须一致。才能赋值
        BeanUtils.copyProperties(areLocationUpdateVo, areLocationVo2);
        Date nowDate = new Date();

        areLocationVo.setUpdateName("小明");
        areLocationVo.setUpdateTime(nowDate);
        int update = areLocationMapper.update(areLocationVo2);
        if (update == 0){
            return ResultBuilderVo.error("请刷新后重试！");
        }

        return ResultBuilderVo.success();

    }

    @Override
    public ResultVo see(Integer id) {
        AreLocationVo areLocationVo = areLocationMapper.queryById(id);
        if (areLocationVo == null){
            return ResultBuilderVo.error("操作对象不存在！");
        }
        return ResultBuilderVo.success(areLocationVo);
    }

    @Override
    public ResultVo queryPage(AreLocationSelectVo areLocationSelectVo) {
        AllRecords allRecords = new AllRecords();
        //根据条件查询总条数
        int count = areLocationMapper.queryCount(areLocationSelectVo);
        //根据条件查询数据
        List<AreLocationVo> areLocationVoList = areLocationMapper.queryDate(areLocationSelectVo);
        for (AreLocationVo areLocationVo: areLocationVoList) {
            if (!areLocationVo.getStatus()){
                areLocationVo.setStatusName("禁用");
            }else {
                areLocationVo.setStatusName("启用");
            }
        }
        allRecords.setPageIndex(areLocationSelectVo.getPageIndex());//当前页
        allRecords.setPageSize(areLocationSelectVo.getPageSize());//每页尺寸（条数）
        allRecords.setTotalNumber(count);//总条数
        allRecords.setDataList(areLocationVoList);//分页的列表数据
        allRecords.resetTotalNumber(count);//总数量
        return ResultBuilderVo.success(allRecords);
    }

    @Override
    public ResultVo updateStatus(@Valid AreLocationUpdateStatusVo areLocationUpdateStatusVo) {
        AreLocationVo areLocationVo = areLocationMapper.queryById(areLocationUpdateStatusVo.getId());
        if (areLocationVo == null){
            return ResultBuilderVo.error("操作对象不存在！");
        }
        AreLocationVo areLocationVo1 = new AreLocationVo();
        BeanUtils.copyProperties(areLocationUpdateStatusVo,areLocationVo1);
        Date nowDate = new Date();
        areLocationVo.setUpdateName("小明");
        areLocationVo.setUpdateTime(nowDate);
        areLocationMapper.updateStatus(areLocationVo);
        return ResultBuilderVo.success();
    }

    private ResultVo vlidate(String name) {

        //验证名称不能重复
        int countName = areLocationMapper.queryByName(name);
        if (countName > 0) {
            return ResultBuilderVo.error("库房标识名称已经存在！");
        }
        return ResultBuilderVo.success();
    }
}
